<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div>
    选择颜色：<select id="colorSelect">
      <option value="">请选择</option>
      <option value="red">红色</option>
      <option value="blue">蓝色</option>
    </select>
  </div>
  <div>
    输入购买数量：<input id="numberInput" type="text" />
  </div>
  <div>
    你选择了颜色：<span id="colorInfo"></span>
  </div>
  <div>
    您输入了数量：<span id="numberInfo"></span>
  </div>
  <button id="nextBtn" disabled="true">请选择手机和购买数量</button>
<script>
let colorSelect = document.getElementById('colorSelect');
let numberInput = document.getElementById('numberInput');
let colorInfo = document.getElementById('colorInfo');
let numberInfo = document.getElementById('numberInfo');
let nextBtn = document.getElementById('nextBtn');
let goods = {
  red: 3,
  blue: 6
}
colorSelect.onchange = () => {
  let color = colorSelect.value;
  let number = numberInput.value;
  let stock = goods[color];
  colorInfo.innerHTML = color;
  if(!color) {
    nextBtn.disabled = true;
    nextBtn.innerHTML = '请选择手机颜色';
    return;
  }
  // 用户输入的购买数量是否为正整数
  if(number <= 0) {
    nextBtn.disabled = true;
    nextBtn.innerHTML = '请输入正确的购买数量';
    return;
  }
  // 当前选择数量没有超过库存量
  if(number > stock) {
    nextBtn.disabled = true;
    nextBtn.innerHTML = '库存不足';
    return;
  }
  nextBtn.disabled = false;
  nextBtn.innerHTML = '放入购物车';
}
numberInput.oninput = () => {
  let color = colorSelect.value;
  let number = numberInput.value;
  let stock = goods[color];
  numberInfo.innerHTML = number;
  if(!color) {
    nextBtn.disabled = true;
    nextBtn.innerHTML = '请选择手机颜色';
    return;
  }
  // 用户输入的购买数量是否为正整数
  if(number-0 <= 0) {
    nextBtn.disabled = true;
    nextBtn.innerHTML = '请输入正确的购买数量';
    return;
  }
  // 当前选择数量没有超过库存量
  if(number > stock) {
    nextBtn.disabled = true;
    nextBtn.innerHTML = '库存不足';
    return;
  }
  nextBtn.disabled = false;
  nextBtn.innerHTML = '放入购物车';
}
</script>
</body>
</html>